package com.chrray.position.service.position.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chrray.position.exception.ServiceException;
import com.chrray.position.mapper.position.FdUserFavoritesMapper;
import com.chrray.position.service.position.FdUserFavoritesService;
import lombok.AllArgsConstructor;
import com.chrray.position.domain.FdUserFavorites;
import com.chrray.position.domain.vo.FdUserFavoritesVO;
import com.chrray.position.domain.dto.search.FdUserFavoritesSearch;
import com.chrray.position.domain.dto.FdUserFavoritesDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户收藏表 接口实现类
 *
 * @author wusiwee
 * @email wusiwee@163.com
 * @since 1.0.1 2024-08-07
 */
@Service
@AllArgsConstructor
public class FdUserFavoritesServiceImpl extends ServiceImpl<FdUserFavoritesMapper, FdUserFavorites> implements FdUserFavoritesService {

    /**
     * 分页查询
     * @param param 查询条件
     * @return 分页列表
     */
    @Override
    public IPage<FdUserFavoritesVO> pageList(FdUserFavoritesSearch param) {
        //查询数据
        IPage<FdUserFavorites> page = page(getPage(param), getWrapper(param));
        IPage<FdUserFavoritesVO> voPage = new Page<>();
        //数据组装
        voPage.setRecords(page.getRecords().stream().map(r->{
            FdUserFavoritesVO vo = new FdUserFavoritesVO();
            BeanUtils.copyProperties(r,vo);
            //数据组装
            setViewParam(r,vo);
            return vo;
        }).collect(Collectors.toList()));
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());
        return voPage;
    }

    /**
     * 动态条件构建
     * @param param 查询参数
     * @return 条件语句
     */
    private LambdaQueryWrapper<FdUserFavorites> getWrapper(FdUserFavoritesSearch param){
        LambdaQueryWrapper<FdUserFavorites> wrapper = Wrappers.lambdaQuery();
        // 主键ID
        wrapper.eq(param.getId() != null, FdUserFavorites::getId, param.getId());
        // 岗位ID
        wrapper.eq(param.getJobId() != null, FdUserFavorites::getJobId, param.getJobId());
        // 用户ID
        wrapper.eq(param.getUserId() != null, FdUserFavorites::getUserId, param.getUserId());
        //
        wrapper.eq(param.getDelFlag() != null, FdUserFavorites::getDelFlag, param.getDelFlag());
        // 主键倒序
        wrapper.orderByDesc(FdUserFavorites::getId);
        return wrapper;
    }

    /**
     * 详情
     * @param id 查询条件
     * @return 详情
    */
    @Override
    public FdUserFavoritesVO info(Long id) {
        // 查询主键详情
        FdUserFavorites entity = getById(id);
        if (ObjectUtils.isEmpty(entity)){
            throw new ServiceException("查询不到数据");
        }
        // 回显参数封装
        FdUserFavoritesVO result = new FdUserFavoritesVO();
        BeanUtils.copyProperties(entity,result);
        // 数据组装
        setViewParam(entity,result);
        return result;
    }

    /**
     * 新增
     * @param body 参数
     * @return 成功与否
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveOrUpdate(FdUserFavoritesDTO body) {
        if (body.getId() == null){
            // 主键为空
            FdUserFavorites entity = new FdUserFavorites();
            BeanUtils.copyProperties(body,entity);
            // 新增
            return save(entity);
        }else {
            // 主键不为空
            FdUserFavorites entity = getById(body.getId());
            BeanUtils.copyProperties(body,entity);
            // 更新
            return updateById(entity);
        }
    }

    /**
     * 批量删除
     * @param idList 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> idList) {
        //逻辑删除
        //FdUserFavorites entity = new FdUserFavorites();
        //entity.setDelFlag(1);
        //return update(entity,new LambdaQueryWrapper<FdUserFavorites>().in(FdUserFavorites::getId,idList));

        //物理删除
        return removeByIds(idList);
    }

    /**
     * 获取分页对象
     *
     * @param param 分页参数
     */
    protected IPage<FdUserFavorites> getPage(FdUserFavoritesSearch param) {
        return new Page<>(param.getPageNum(), param.getPageSize());
    }

    /**
     * 视图数据赋值
     * @param r 数据
     * @param vo 视图
     */
    private void setViewParam(FdUserFavorites r, FdUserFavoritesVO vo) {
        //数据赋值业务..
    }

}
